package com.advertisementsystem.Controller;

import com.advertisementsystem.Dto.PermissionDTO;
import com.advertisementsystem.Exception.ResponseResult;
import com.advertisementsystem.Servie.PermissionService;
import com.advertisementsystem.entity.Permission;
import com.advertisementsystem.vo.PermissionTreeVO;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/permission")
@Validated
public class PermissionController {

    @Autowired
    private PermissionService permissionService;

    /**
     * 获取权限树结构
     */
    @GetMapping("/tree")
    public ResponseResult<List<PermissionTreeVO>> getPermissionTree() {
        return ResponseResult.success(permissionService.getPermissionTree());
    }

    /**
     * 获取所有权限列表
     */
    @GetMapping("/list")
    public ResponseResult<List<Permission>> getAllPermissions() {
        return ResponseResult.success(permissionService.getAllPermissions());
    }

    /**
     * 添加权限
     */
    @PostMapping
    public ResponseResult<?> addPermission(@RequestBody @Valid PermissionDTO permissionDTO) {
        permissionService.addPermission(permissionDTO);
        return ResponseResult.success("");
    }

    /**
     * 更新权限
     */
    @PutMapping("/{id}")
    public ResponseResult<?> updatePermission(@PathVariable Long id,
                                              @RequestBody @Valid PermissionDTO permissionDTO) {
        permissionService.updatePermission(id, permissionDTO);
        return ResponseResult.success("");
    }

    /**
     * 删除权限
     */
    @DeleteMapping("/{id}")
    public ResponseResult<?> deletePermission(@PathVariable Long id) {
        permissionService.deletePermission(id);
        return ResponseResult.success("");
    }

    /**
     * 更新权限状态
     */
    @PutMapping("/{id}/status")
    public ResponseResult<?> updatePermissionStatus(@PathVariable Long id, @RequestParam Integer status) {
        permissionService.updatePermissionStatus(id, status);
        return ResponseResult.success("");
    }
}